package com.cq.oa.common.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import oa.base.spring.ApplicationPropertiesHolder;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

/**
 * 
 * @author lilang
 * 2013-6-27上午9:12:39
 */
public class Convert2swf {
	// 转换文件list
	public static List<String> urlList = new ArrayList<String>();
	// OpenOffice的安装目录,
	public static String OPENOFFICE_HOME = ApplicationPropertiesHolder.getProperty("openoffice.home");
	// 工具文件夹路径
	public static String SWFTOOLS_PATH = ApplicationPropertiesHolder.getProperty("swftools.path");
	// 附件路径
	public static String FILE_PATH = ApplicationPropertiesHolder.getProperty("up.load.file.url");
	// swftools 语言包路径
	public static String SWFTOOLS_LANGUAGE = ApplicationPropertiesHolder.getProperty("swftools.language");
	// 允许转换的类型
	public static String SWF2TYPE=ApplicationPropertiesHolder.getProperty("swf2Type");
	
	private static DefaultOfficeManagerConfiguration config;
	private static OfficeManager officeManager;
	private static OfficeDocumentConverter converter;
	/**
	 * 初始化打开 openOffice
	 * 
	 * lilang 
	 * 2013-6-27上午9:12:39
	 */
	private static void init(){
		if(null==config){
			config = new DefaultOfficeManagerConfiguration();
			config.setOfficeHome(OPENOFFICE_HOME);
			int port = (new Random()).nextInt(9999);
			if (port <= 1024){
				port += 1024;
			}
			config.setPortNumber(port);
		}
		if(null==officeManager){
			officeManager = config.buildOfficeManager();
			try {
				officeManager.start();
			} catch (Exception e) {
				officeManager.stop();
				officeManager.start();
			}
		}
	}
	private static OfficeDocumentConverter getConverter(){
		if(null==converter){
			init();
			converter = new OfficeDocumentConverter(officeManager);
			
		}
		return converter;
	}
	/**
	 * 关闭 office 控制 重新启动
	 * 
	 * lilang 
	 * 2013-6-27上午9:30:25
	 */
	public static void stopOfficer(){
		if(null!=officeManager){
			try {
				converter = null;
				officeManager.stop();
				officeManager.start();
			} catch (Exception e) {
				converter = null;
				officeManager.stop();
				officeManager.start();
			}
		}
	}
	
	/**
	 * 批量转换swf
	 * @param urlList
	 * @throws Exception
	 */
	public static void convert2Swf(List<String> urlList) throws Exception {
		for (String sourcePath : urlList) {
			if(!sourcePath.startsWith(FILE_PATH)){
				sourcePath = FILE_PATH +"\\" + sourcePath;
			}
			File sourceFile = new File(sourcePath);
			if (sourceFile.exists()) {// 找不到源文件, 则返回
				String tempfile = sourcePath.toLowerCase();
				String fix = tempfile.substring(tempfile.lastIndexOf(ToolUtils.PIONT));
				// 转换office文档
				if (SWF2TYPE.indexOf(fix)>=0) {
						File pdfFile = new File(sourcePath + ToolUtils.PDF);
						File swfFile = new File(sourcePath + ToolUtils.SWF);
						// 转换文档
						//不存在他的 pdf 才转换如果存在
						office2pdf(sourceFile,pdfFile);
						pdf2swf(pdfFile, swfFile);
					// 转换pdf
				} else if (ToolUtils.PDF.equals(fix)) {
					File pdfFile = new File(sourcePath);
					File swfFile = new File(sourcePath + ToolUtils.SWF);
					pdf2swf(pdfFile, swfFile);
				}
			}
		}
		
		//stopOfficer();
	}

	/**
	 * office文件转换pdf
	 * 
	 * @param sourceFile
	 * @param destFile
	 */
	private static void office2pdf(File sourceFile, File pdfFile) {
		//存在文件不需要转换
		if(!pdfFile.exists()){
			getConverter().convert(sourceFile, pdfFile);
		}
	}
	/**
	 * pdf 转 swf
	 * @param pdfFile
	 * @param swfFile
	 * @throws Exception
	 * lilang 
	 * 2013-6-27上午9:24:45
	 */
	private static void pdf2swf(File pdfFile, File swfFile) throws Exception{
		if(!swfFile.exists()){
			pdf2swf(pdfFile.getPath(), swfFile.getPath());
		}
	}
	
	
	
	/**
	 * pdf文件转成swf
	 * 
	 * @param sourcePath
	 * @param destpath
	 * @throws Exception
	 */
	private static void pdf2swf(String sourcePath, String destpath) throws Exception {
		File source = new File(sourcePath);
		if (!source.exists()) {
			return;
		}
		// pdf转成swf
		String[] envp = new String[1];
		envp[0] = "PATH=" + SWFTOOLS_PATH;
		String command = "pdf2swf  -z -s flashversion=9 " + sourcePath + " -s languagedir=" + SWFTOOLS_LANGUAGE
				+ "xpdf-win32/xpdf-chinese-simplified -o  " + destpath + "  -T 9";
		Process pro = Runtime.getRuntime().exec(command);
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream()));
		while (bufferedReader.readLine() != null) {
			bufferedReader.readLine();
		}
		pro.waitFor();
		pro.exitValue();
	}
	/**
	 * 单个office转成swf
	 * 
	 * @param sourcePath
	 * @throws Exception
	 */
	public static void office2swf(String sourcePath) throws Exception {
		File sourceFile = new File(sourcePath);
		if (!sourceFile.exists()) {
			return;
		}
		File pdfFile = new File(sourcePath + ToolUtils.PDF);
		File swfFile = new File(sourcePath + ToolUtils.SWF);

		String tempfile = sourcePath.toLowerCase();
		String fix = tempfile.substring(tempfile.lastIndexOf(ToolUtils.PIONT));
		if (SWF2TYPE.indexOf(fix)>=0) {
			if (sourceFile.exists()) {// 找不到源文件, 则返回
					office2pdf(sourceFile, pdfFile);
					pdf2swf(pdfFile, swfFile);
			}
		} else if (ToolUtils.PDF.equals(fix)) {
			pdf2swf(pdfFile, swfFile);
		}
		//stopOfficer();
	}

	public static void main(String[] args) {
		try {
			List<String> urls = new ArrayList<String>();
			urls.add("129.doc");
			convert2Swf(urls);
			// office2swf("/uie-resource/fly.com/INF_BULLETIN/UIE20130524153105620231067F04AA2.doc");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
